package com.visikard.widget;

import android.widget.RelativeLayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;

public class AnimationRelativeLayout extends RelativeLayout
{
    Context context;
    AnimationRelativeLayout _me;
    int duration = 200;
    boolean _fromTopToDown = false;
    public AnimationRelativeLayout(Context context)
    {
        super(context);
        this.context = context;
        _me = this;
    }

    public AnimationRelativeLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context = context;
        _me = this;
    }
    
    public void hide(boolean anim, boolean fromTopToDown)
    {
    	_fromTopToDown = fromTopToDown;
        if (!anim)
        {
            _me.setVisibility(View.GONE);
            return;
        }
        if(_fromTopToDown == true)
        {
        	hideFromTopToDown();
        }
        else
        {
        	hideFromDownToTop();
        }
    }
    
    public void show(boolean anim, boolean fromTopToDown)
    {
    	_fromTopToDown = fromTopToDown;
        if (!anim)
        {
            _me.setVisibility(VISIBLE);
            return;
        }
        _me.setVisibility(VISIBLE);
        isShowing = true;
    }

    private boolean isShowing = false;

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        if (isShowing)
        {
        	if(_fromTopToDown == true)
        	{
        		showFromTopToDown();
        	}
        	else
        	{
        		showFromDownToTop();
        	}
//            TranslateAnimation ta = new TranslateAnimation(
//                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
//                    0, Animation.ABSOLUTE, _me.getMeasuredHeight(),
//                    Animation.RELATIVE_TO_SELF, 0f);
//            ta.setDuration(duration);
//            ta.setInterpolator(new LinearInterpolator());
//            _me.startAnimation(ta);
//            isShowing = false;
        }
    }
    
    //duy
    public void hideFromTopToDown()
    {
    	 TranslateAnimation ta = new TranslateAnimation(
                 Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                 Animation.RELATIVE_TO_SELF, 0f, Animation.ABSOLUTE,
                 _me.getHeight());

         ta.setAnimationListener(new AnimationListener()
         {
             @Override
             public void onAnimationStart(Animation animation)
             {
             }

             @Override
             public void onAnimationRepeat(Animation animation)
             {
             }

             @Override
             public void onAnimationEnd(Animation animation)
             {
                 _me.setVisibility(View.GONE);
             }
         });
         ta.setDuration(duration);
         ta.setInterpolator(new LinearInterpolator());
         _me.startAnimation(ta);
    }
    
    public void hideFromDownToTop()
    {
    	 TranslateAnimation ta = new TranslateAnimation(
                 Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                 Animation.RELATIVE_TO_SELF, _me.getHeight(), Animation.ABSOLUTE, 0f);

         ta.setAnimationListener(new AnimationListener()
         {
             @Override
             public void onAnimationStart(Animation animation)
             {
             }

             @Override
             public void onAnimationRepeat(Animation animation)
             {
             }

             @Override
             public void onAnimationEnd(Animation animation)
             {
                 _me.setVisibility(View.GONE);
             }
         });
         ta.setDuration(duration);
         ta.setInterpolator(new LinearInterpolator());
         _me.startAnimation(ta);
    }
    
    public void showFromTopToDown()
    {
    	TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                0, Animation.ABSOLUTE, 0f,
                Animation.RELATIVE_TO_SELF, _me.getMeasuredHeight());
        ta.setDuration(duration);
        ta.setInterpolator(new LinearInterpolator());
        _me.startAnimation(ta);
        isShowing = false;
    }
    
    public void showFromDownToTop()
    {
    	TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                0, Animation.ABSOLUTE, _me.getMeasuredHeight(),
                Animation.RELATIVE_TO_SELF, 0f);
        ta.setDuration(duration);
        ta.setInterpolator(new LinearInterpolator());
        _me.startAnimation(ta);
        isShowing = false;
    }
}